﻿
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Quartz;
using Service.Framework.AppStartup;

namespace Service.Core.Quartz;

/// <summary>
/// 
/// </summary>
public class CoreQuartzStartup : StartupModule<CoreQuartzStartup>
{
    public override int Order { get; set; } = 90;
    /// <summary>
    /// 
    /// </summary>
    /// <param name="webApplicationBuilder"></param>
    public override void ConfigureServices(WebApplicationBuilder webApplicationBuilder)
    {
        var services = webApplicationBuilder.Services;

        services.Configure<QuartzOptions>(options =>
        {
            options.SchedulerName = "";

        });

        //注册ISchedulerFactory的实例。
        services.AddQuartz(q =>
        {
            q.UsePersistentStore(s =>
            {
                s.UseProperties = true;
                s.RetryInterval = TimeSpan.FromSeconds(5);
                s.UseMySql(mysql =>
                {
                    mysql.ConnectionString = "server=localhost;uid=root;pwd=wy.023;port=3306;database=mydb;Allow User Variables=True";
                    // this is the default
                    mysql.TablePrefix = "qrtz_";
                });

                s.UseClustering(cluster =>
                {
                    cluster.CheckinMisfireThreshold = TimeSpan.FromSeconds(20);
                    cluster.CheckinInterval = TimeSpan.FromSeconds(10);
                });
                q.UseDefaultThreadPool(tp =>
                {
                    tp.MaxConcurrency = 10;
                });
            });

        });
        services.AddQuartzHostedService(o =>
        {

            o.WaitForJobsToComplete = true;
        });

    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="webApplication"></param>
    public override void Configure(WebApplication webApplication)
    {

        //var _quartzJobService = webApplication.Services.GetService<IQuartzJobService>();
        //if (_quartzJobService is null) return;
        //// 从内存中获取任务列表
        //var memoryJobList = App.JobTaskInfos.Where(w => w.ScheduledAttribute.JobTaskType == QuartzJobTaskType.Memory);
        //foreach (var item in memoryJobList)
        //{
        //    _quartzJobService.RunAsync<QuartzJobEntity, MemoryJob>(new QuartzJobEntity()
        //    {
        //        Cron = item.ScheduledAttribute.Cron,
        //        GroupName = item.ScheduledAttribute.GroupName ?? "default-group",
        //        Name = item.ScheduledAttribute.Name ?? item.Key,
        //        Remark = item.ScheduledAttribute.Remark,
        //        // key 如果放入了 HZY.Api.CalcCenter.ApplicationServices.ServiceBus.Publishs.AppPublish>TestPublishAuto 表示执行函数
        //        JobPoint = item.Key
        //    }).Wait()
        //   ;
        //}

        //// 异步任务
        //var memoryJobAsyncList = App.JobTaskInfos.Where(w => w.ScheduledAttribute.JobTaskType == QuartzJobTaskType.MemoryAsync);
        //foreach (var item in memoryJobAsyncList)
        //{
        //    _quartzJobService.RunAsync<QuartzJobEntity, MemoryJobAsync>(new QuartzJobEntity()
        //    {
        //        Cron = item.ScheduledAttribute.Cron,
        //        GroupName = item.ScheduledAttribute.GroupName ?? "default-group",
        //        Name = item.ScheduledAttribute.Name ?? item.Key,
        //        Remark = item.ScheduledAttribute.Remark,
        //        // key 如果放入了 HZY.Api.CalcCenter.ApplicationServices.ServiceBus.Publishs.AppPublish>TestPublishAuto 表示执行函数
        //        JobPoint = item.Key
        //    }).Wait()
        //   ;
        //}
    }



}
